package com.jack.payment.service.basic.impl;


import com.jack.payment.constant.ResultHintConstant;
import com.jack.payment.dao.config.ConfigEmailDao;
import com.jack.payment.dao.setting.OfficialDao;
import com.jack.payment.dao.setting.SettingRuleDao;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.filter.RestFilter;
import com.jack.payment.entity.generate.config.ConfigEmailModel;
import com.jack.payment.entity.generate.setting.OfficialModel;
import com.jack.payment.entity.generate.setting.SettingRuleModel;
import com.jack.payment.util.auth.DataAuthUtils;
import com.jack.payment.util.common.EmailMessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectConfigServiceImpl {
    private Logger log = LoggerFactory.getLogger(ProjectConfigServiceImpl.class);

    @Autowired
    private EmailMessageUtil emailMessageUtil;
    @Autowired
    private ConfigEmailDao configEmailDao;
    @Autowired
    private DataAuthUtils dataAuthUtils;
    @Autowired
    private SettingRuleDao settingRuleDao;
    @Autowired
    private OfficialDao officialDao;

    /**
     * 配置项目的发送邮箱
     * @param id
     */
    public void configProjectSendEmail(String id) {
        ConfigEmailModel configEmailModel = dataAuthUtils.decodeConfigEmail(configEmailDao.getAppointConfigEmail(id));
        emailMessageUtil.initConfigEmail(configEmailModel, emailMessageUtil.configMailInfo(configEmailModel));
    }

    /**
     * 系统规则列表
     * @return
     */
    public List<SettingRuleModel> getSettingRuleList() throws BusinessException {
        List<SettingRuleModel> settingRuleModelList = settingRuleDao.getSettingRuleList();
        if (settingRuleModelList.isEmpty()) {
            log.warn("规则内容为空");
            throw new BusinessException(ResultHintConstant.BASIC_DATA_DROP);
        }
        return settingRuleModelList;
    }

    /**
     * 筛选消息数据 总数
     * @param officialFilter
     * @return
     * @throws BusinessException
     */
    public Integer getFilterOfficialCount(RestFilter.OfficialFilter officialFilter) throws BusinessException {
        return officialDao.getFilterOfficialMessageCount(officialFilter);
    }

    /**
     * 筛选消息 列表
     * @param officialFilter
     * @return
     * @throws BusinessException
     */
    public List<OfficialModel> getFilterOfficialList(RestFilter.OfficialFilter officialFilter) throws BusinessException {
        List<OfficialModel> officialList = officialDao.getFilterOfficialMessageList(officialFilter);
        if (officialList.isEmpty()) {
            log.warn("文案内容为空");
            throw new BusinessException(ResultHintConstant.BASIC_DATA_DROP);
        }
        return officialList;
    }

    /**
     * 修改系统文案内容
     * @param officialModel
     */
    public void updateAppointOfficial(OfficialModel officialModel) {
        officialDao.updateOfficialMessageContent(officialModel);
    }
}
